In [149]:
import struct
def printfloat(f):
b= ''.join(bin(c).replace('0b', '').rjust(8, '0') for c in struct.pack('!d', f))
print("%+.18f %s %s %s "%(f,b[0],b[1:12],b[12:]))
In [150]:
def f(x):
return ((x+1.)-1.)/x
In [151]:
f(1e-14)
Out[151]:
In [152]:
printfloat(1.)
printfloat(1.e-14)
printfloat(1.+1.e-14)
printfloat((1.+1.e-14)-1.0)
printfloat(((1.+1.e-14)-1.0)/1e-14)
In [153]:
def g(x):
for i in range(620):
x = x*10
return x
In [154]:
g(1.1)
Out[154]:
In [155]:
printfloat(1.1)
printfloat(1.1*2.)
printfloat(1.1*2.*2.*2.*2.*2.)
x = 1.1
for i in range(1023):
x = x*2.
printfloat(x)
printfloat(2.*x)
In [32]:
2.**1023
Out[32]:
In [35]:
import numpy as np
In [37]:
pi = np.pi
In [40]:
a = [1.,2.,3.,4.]
In [43]:
b = np.array([1.,2.,3.,4.])
In [45]:
2.*b+7.
Out[45]:
In [47]:
c = np.zeros(1000)
In [48]:
c[0] = 9
In [50]:
c[999] = 7.
In [51]:
c
Out[51]:
In [56]:
np.linspace(0.,10.,10)
Out[56]:
In [57]:
np.zeros(10,dtype=int)
Out[57]:
In [58]:
%matplotlib inline
import matplotlib.pyplot as plt
In [100]:
fig, ax = plt.subplots(1,1)
ax.plot(np.array([1,2]),np.array([3,4]))
ax.plot(np.array([1,2]),np.array([7,2]))
ax.scatter(np.array([1,2]),np.array([3,4]))
ax.scatter(np.array([1,2]),np.array([7,2]))
Out[100]:
In [156]:
x = np.linspace(0.,10.,300)
In [157]:
x
Out[157]:
If it's a numpy function:
In [73]:
sinx = np.zeros(len(x))
Doing it element-wise by hand:
In [158]:
for i in range(len(x)):
sinx[i] = np.sin(x[i])+np.random.random() #adding some random numbers to simulate noise here
In [159]:
fig, ax = plt.subplots(1,1)
ax.scatter(x,sinx)
Out[159]:
In [160]:
N = 100
grid = np.zeros((N,N))
In [161]:
grid[0][0] = 1.
grid[1][0] = 2.
grid[2,0] = 3.
In [162]:
grid
Out[162]:
In [163]:
for i in range(grid.shape[0]):
for j in range(grid.shape[1]):
grid[i][j] = np.sin(0.1*np.sqrt((i-50.)**2.+(j-50.)**2))
In [164]:
grid
Out[164]:
In [165]:
fig, ax = plt.subplots(1,1)
ax.plot(grid[0])
Out[165]:
In [166]:
fig, ax = plt.subplots(1,1)
img1 = ax.imshow(grid)
cb = fig.colorbar(img1)
cb.set_label("temperature [C]")
ax.set_ylabel("y [m]")
ax.set_xlabel("x [m]")
Out[166]:
In [167]:
fig, ax = plt.subplots(1,1)
img1 = ax.imshow(grid,cmap="jet")
cb = fig.colorbar(img1)
cb.set_label("temperature [C]")
ax.set_ylabel("y [m]")
ax.set_xlabel("x [m]")
Out[167]:
In [ ]: